package sample.stormax.pres.news.utils.sharepres;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.Map;

import sample.stormax.pres.news.utils.sharepres.ISharePreferenceOperation;

/**
 * Created by StormAX on 2018/7/3 11:22.
 * Mail yuanmenglong97@qq.com
 */

public class SPUtils implements ISharePreferenceOperation {

    private static final String SP_NAME = "types";
    private static final String LOAD_STATE = "load";

    private static final String FIRST_LOAD = "firstLoad";


    private SharedPreferences settings;
    private SharedPreferences loadState;

    private SharedPreferences.Editor editor;

    public SPUtils(Context context) {
        settings = context.getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);
        loadState = context.getSharedPreferences(LOAD_STATE,Context.MODE_PRIVATE);
    }

    @Override
    public void addTypes(ArrayList<String> types) {
        editor = settings.edit();
        for (int i = 0; i < types.size(); i++) {
            editor.putString(i+"",types.get(i));
        }
        editor.apply();
    }

    @Override
    public ArrayList<String> getTypes() {
        Map<String,String> map = (Map<String, String>) settings.getAll();
        ArrayList<String> types = new ArrayList<>();
        for (int i = 0; i < map.size(); i++) {
            types.add(map.get(i + ""));
        }
        return types;
    }

    @Override
    public void firstLoad() {
        editor = loadState.edit();
        editor.putBoolean(FIRST_LOAD,false);
        editor.apply();
    }

    @Override
    public boolean isFirstLoad() {
        return loadState.getBoolean(FIRST_LOAD,true);
    }
}
